using System.Collections.Generic;
using UnityEngine;

namespace Gj
{
    public enum RUITypte
    {
        None,
        Click,
        Drag,
        All,
    }
    public static class SystemInput
    {
        public static float scale = 1;
        public static int level;
        public static float y;
        public static float process;
        public static Vector3 size;
        public static Vector3 area;
        public static Vector3 center;
        public static Vector2 minArea;
        public static Vector2 maxArea;
        public static Vector2 screenMax;
        public static Vector2 screenMin;
        public static Vector2 screenCenter;
        public static Vector2 screenSize;
        public static Vector2 canvasCenter;
        public static Vector2 canvasSize;
        public static Vector2 resolution;
        public static float ppi = 1;
        public static float dprX = 1;
        public static float dprY = 1;
        public static bool full;
        public static ScreenOrientation screenOrientation;
        public static float view = 60;
        public static float moveDistance = 15f;
        public static float clickTime = 0.3f;
        public static float loopTime = 0.5f;
        public static float waitTime = 0.15f;
        public static bool lo;
        public static float lh;
        public static float lv;
        public static bool ro;
        public static float rh;
        public static float rv;
        public static bool so;
        public static float sh;
        public static float sv;
        public static float si;
        public static float sr; // 旋转角度
        public static bool sm;
        public static float sx;
        public static float sy;
        public static float bx;
        public static float by;
        public static bool sb;
        public static float sz;
        public static Vector2 cp;
        public static RUITypte rUI;
        public static ScreenHandle.HandleAction handle;
        public static Dictionary<string, Texture> image = new Dictionary<string, Texture>();

        public static void Init()
        {
            PPI();
            DPR();
        }

        static void PPI()
        {
            // 获取屏幕对角线尺寸（英寸）
            float screenInches = Mathf.Sqrt(
                Mathf.Pow(Screen.width / Screen.dpi, 2) +
                Mathf.Pow(Screen.height / Screen.dpi, 2)
            );

            if (screenInches > 0)
            {
                // 计算 PPI
                ppi = Mathf.Sqrt(
                    Mathf.Pow(Screen.currentResolution.width, 2) +
                    Mathf.Pow(Screen.currentResolution.height, 2)
                ) / screenInches;
            }
        }

        static void DPR()
        {
            // 物理分辨率（真实像素）
            int physicalWidth = Screen.currentResolution.width;
            int physicalHeight = Screen.currentResolution.height;

            // 逻辑分辨率（Unity 使用的分辨率，受 Canvas Scaler 影响）
            int logicalWidth = Screen.width;
            int logicalHeight = Screen.height;

            // 计算设备像素比（DPR）
            dprX = (float)physicalWidth / logicalWidth;
            dprY = (float)physicalHeight / logicalHeight;
        }
        
        public static void Add(string key, Texture t)
        {
            if (image.ContainsKey(key))
            {
                image[key] = t;
            }
            else
            {
                image.Add(key, t);
            }
        }

        public static void SetArea(Vector3 c, Vector3 s)
        {
            center = c;
            size = s;
            area = s * 1.5f;
            minArea = new Vector2((center.x - (area.x / 2)), (center.z - (area.z / 2)));
            maxArea = new Vector2((center.x + (area.x / 2)), (center.z + (area.z / 2)));
        }

        public static bool CheckInArea(Vector3 position)
        {
            Vector2 p = MathTools.ToVector2(position);
            if (p.x > minArea.x && p.y > minArea.y && p.x < maxArea.x && p.y < maxArea.y) return true;
            return false;
        }
        
        public static bool CheckInScreen(Vector2 point)
        {
            return point.x > screenMin.x && point.x < screenMax.x && point.y > screenMin.y &&
                   point.y < screenMax.y;
        }
    }
}
